<?php

namespace App\Http\Controllers;

use App\Models\ArticleModel;

class ArticleController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => []]);
    }

    /**
     * Get a JWT via given credentials.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail()
    {

        $credentials = request(['article_id']);

        if(!empty($credentials['article_id'])){
            $data = ArticleModel::where(['id'=>$credentials['article_id']])->select(['id','title','cover','type','title_content','content','created_at'])->first();
        }
        if(empty($data)){
            $data = array();
        }

        ArticleModel::where(['id'=>$credentials['article_id']])->increment('read');

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function listing()
    {
        $credentials = request(['type','page']);

        $page = empty($credentials['page'])?0:$credentials['page'];

        $limit = 5;

        $type = empty($credentials['type']) ? 1 : $credentials['type'];

        $data['topping_list'] = ArticleModel::where(['type'=>$type,'topping'=>1])->select(['id','title','cover','type','title_content','topping','read','created_at'])->orderBy('created_at','DESC')->get();

        $data['list'] = ArticleModel::where(['type'=>$type,'topping'=>0])->select(['id','title','cover','type','title_content','topping','read','created_at'])->offset($page*$limit)->limit($limit)->orderBy('created_at','DESC')->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

}
